﻿using System;

using System.Data;
using Epm.AppLogic.Framework;

namespace msBsc.WebUi
{
    public partial class MenuTree : msBsc.Cls.BPage
    { 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
            }
            else
            {
                //this.ExecJScript("initVoy();");
            }
        }
        private void BindControls()
        {
            if (!IsPostBack)
            {
                //PageUtils.BoundComboBox(dplUSER_TYPE_CID, this.DataConst, "CONSTANT_DISP_TEXT", "CONSTANT_VALUE", "CONSTANT_CATEGORY_CID = 'EPM_EXEC_USER_FILTER_TYPE_CID'", "SEQ_NUM ASC", true);
            }
        }
         

        public DataSet GetAllUserBseMenu(string strMENU_TYPE)
        {
            string sqlString = " SELECT * FROM SYS_MENU_CONFIG WHERE (REC_ACTIVE = 1) AND (MENU_TYPE = '" + strMENU_TYPE + "') ORDER BY MENU_SEQ_ON_LEVEL ASC";
            try
            {
                using (CDbAccess oCDbAccess = new CDbAccess())
                {
                    DataSet ds = oCDbAccess.QueryBySQL(sqlString, null);
                    ds.Tables[0].TableName = "SYS_MENU_CONFIG";
                    return ds;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public DataSet SelecSysMenuByUser(
            string strUser, string strMENU_TYPE)
        {
            try
            {
                using (CDbAccess oCDbAccess = new CDbAccess())
                {
                    DataSet ds = oCDbAccess.QueryBySQL("SELECT DISTINCT B.* FROM SYS_USER_BELONG_ROLE R LEFT OUTER JOIN  SYS_MENU_RELATION A ON R.SYS_USER_ROLE_ID=A.RELATION_ID LEFT OUTER JOIN SYS_MENU_CONFIG B ON A.MENU_CONFIG_ID=B.MENU_CONFIG_ID WHERE B.MENU_CONFIG_ID IS NOT NULL AND R.USER_CDE='" + strUser + "' AND MENU_TYPE='" + strMENU_TYPE + "' ORDER BY B.MENU_SEQ_ON_LEVEL ASC");
                    ds.Tables[0].TableName = "SYS_MENU_CONFIG";
                    return ds;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        //获取数据原,"DP_DATA"字符一定不要改变，否则系统不能自动清除缓存
        public DataSet GetDisplayData(string strgtGridID, string strsRadom)
        {
            //string strtem1 = this.oUserInfo.Operator;
            //if (strtem1 == string.Empty)
            //    strtem1 = "NOT USER_DDF";
            //if (this.oUserInfo.OperRank == "CUST_ADMIN")
            //{
                return GetAllUserBseMenu("0");// this.objSysMenuRelation.SelecSysMenuByUser(this.Operator, "0");
            //}
            //else
            //{
            //    return SelecSysMenuByUser(strtem1, "0");
            //} 
        } 

        public string GetMenuHtml()
        {
            DataSet dsMenuSrc = GetDisplayData("myGrid1", GetParamHtValue("sRadom"));
            string strMenuRet = "<div class=\"menu_head\"></div>";
            if (dsMenuSrc == null && dsMenuSrc.Tables[0].Rows.Count == 0) return "";
            DataRow[] arrRC = dsMenuSrc.Tables[0].Select("IS_ROOT = 'True' and MENU_TYPE ='0' and REC_ACTIVE = 1");
            int iCount = 1;
            foreach (DataRow drC in arrRC)
            {
                strMenuRet += "<div id=\"panel" + drC["MENU_CONFIG_ID"].ToString() + "\" class=\"panel_head\" onmouseover=\"SetImgStyle(this.id,1);\" onmouseout=\"SetImgStyle(this.id,0);\">";
                strMenuRet += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"tab" + drC["MENU_CONFIG_ID"].ToString() + "\" class=\"menu_body\" onClick=\"SetStyle(this.id);\" >";
                strMenuRet += "<tr><td>&nbsp</td>";
                strMenuRet += "<td><img src=\"" + httpUrlBase + "Sys/img/point_white.png\" id=\"img" + drC["MENU_CONFIG_ID"].ToString() + "\" style=\"margin-right: 5px;padding-top:2px;\" /></td>";
                strMenuRet += "<td>" + drC["MENU_NAME"].ToString() + "</td>";
                strMenuRet += "<td>&nbsp</td></tr></table>";
                if (iCount > 200)
                {
                    strMenuRet += "<div class=\"panel_content_hidden\">";
                }
                else
                {
                    strMenuRet += "<div class=\"panel_content\">";
                }
                strMenuRet += GetChildNode(dsMenuSrc, drC["MENU_CONFIG_ID"].ToString());
                strMenuRet += "</div></div>";
                iCount++;
            }
            strMenuRet += "<div class=\"menu_tail\"></div>";
            return strMenuRet;
        }

        private string GetChildNode(DataSet dsMenuSrc, string strMenuConfigId)
        {
            DataRow[] arrRC = dsMenuSrc.Tables[0].Select("PARENT_MENU_ID = '" + strMenuConfigId + "' and REC_ACTIVE = 1");
            string strRet = "";
            if (arrRC.Length > 0)
            {
                foreach (DataRow drC in arrRC)
                {
                    strRet = strRet + "<div class=\"content_item\" id=\"dv" + drC["MENU_CONFIG_ID"].ToString() + "\" onmouseover=\"SetItemImgStyle(this.id,1);\" onmouseout=\"SetItemImgStyle(this.id,0);\" onclick=\"javascript:doChildMenuSHOW('" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(drC["WEB_PAGE_NAME"].ToString()) + "','" + drC["MENU_NAME"].ToString() + "','" + drC["NEW_WINDOW"].ToString() + "');return false;\"><a href=\"#fragment1\" onclick=\"javascript:doChildMenuSHOW('" + msBsc.AppLogic.Cfg.HttpTools.stringToJson(drC["WEB_PAGE_NAME"].ToString()) + "','" + drC["MENU_NAME"].ToString() + "','" + drC["NEW_WINDOW"].ToString() + "');return false;\"><img id=\"img" + drC["MENU_CONFIG_ID"].ToString() + "\" src=\"" + httpUrlBase + "Sys/img/offline.png\" />" + drC["MENU_NAME"].ToString() + "</a></div>";
                }
            }
            return strRet;
        }

        public override void InitialByParam()
        {
            //if (strAction == "mShowMenu")
            //{
            //    ShowData();
            //}

            base.InitialByParam();
        }

    }
}
